<?php
include('../data/comm.inc.php');
include('../data/myadminvar.php');
include('../func/func.php');
include('../func/csfunc.php');
include('../func/adminfunc.php');
include('../include.php');
include('./checklogin.php');
switch ($_REQUEST['xtype']) {
    case "show":
	   //error_reporting(E_ALL);
        $qishu    = array();
        $qishu[0] = $config['thisqishu'];
	
        $msql->query("select qishu from `$tb_kj` where gid='$gid' and m1!=''   order by kjtime desc");//and kjtime>$time
        $i = 1;
        while ($msql->next_record()) {
            $qishu[$i] = $msql->f('qishu');
            $i++;
        }
		
	    	$qishu = fast3qishu($qishu);
		
        $tpl->assign('qishu', $qishu);
        $tpl->assign("b", getb());
        $tpl->assign("topid", $userid);
        $tpl->assign("layer", transuser($userid, 'layer'));
        $tpl->assign("username", transuser($userid, 'username'));
		$msql->query("select wid,layer,namehead from `$tb_web` order by wid");
		$i=0;
		while($msql->next_record()){
			$layer[$i]['wid'] = $msql->f('wid');
			$layer[$i]['layer'] = json_decode($msql->f('layer'),true);
			$namehead = json_decode($msql->f('namehead'),true);
			$layer[$i]['namehead'] = $namehead[0];
			$i++;
		}
        $tpl->assign("layername", $layer);
        $tpl->assign("topuser", topuser($userid));
        $sdate = week();
        $tpl->assign("sdate", $sdate);
        $tpl->display("xxtz2.html");
        break;
    case "gettzxx":
        $bid   = $_POST['bid'];
        $cid   = $_POST['cid'];
        $sid   = $_POST['sid'];
	
		$qishu=explode('~',$_POST['qishu']);
		$q1 = trim($qishu[0]);
		$q2 = trim($qishu[1]);
		
        $uid   = $_POST['uid'];
        $page  = $_POST['page'];
        if (!is_numeric($page))
            $page = 1;
        $psize = $config['psize3'];
        $fs    = $_POST['fs'];
        $start = rdates($_POST['start']);
        $end   = rdates($_POST['end']);
        if ($fs == 1) {
            $whi = " and B.gid='$gid' and B.qishu>=$q2 and B.qishu<=$q1  ";//and B.baostatus=1
        } else {
        
            $whi   = " and B.gid='$gid' and B.dates>='".$start."' and B.dates<='".$end."' ";//and B.baostatus=1 
        }
        $yq = "  ";
        if ($bid != '' & $bid != 'null') {
            $yq .= " and A.bid='$bid' ";
        }
        if ($sid != '' & $sid != 'null') {
            $yq .= " and A.sid='$sid' ";
        }
        if ($cid != '' & $cid != 'null') {
            $yq .= " and A.cid='$cid' ";
        }
        $join = " from `$tb_lib` as A  Left join `$tb_kj` as B on A.qishu=B.qishu and A.gid=B.gid where 1=1 $yq $whi ";
        $sql  = " select count(A.id) $join and A.userid='$uid'";
        $msql->query($sql);
        $msql->next_record();
        $rcount = pr0($msql->f(0));
        $pcount = $rcount % $psize == 0 ? $rcount / $psize : (($rcount - $rcount % $psize) / $psize) + 1;
        $pstr   = page($pcount, $page);
        $sql    = " select A.* $join and A.userid='$uid' order by A.xtype,A.time desc,A.id desc ";
        $sql .= " limit " . ($page - 1) * $psize . "," . $psize;
        $msql->query($sql);
        $tz = array();
        $i  = 0;
		$tmp = array();
        $fl = transgame($gid,'fenlei');
        while ($msql->next_record()) {

            $tz[$i]['xtype']  = transxtype($msql->f('xtype'));
            $tz[$i]['id']    = $msql->f('id');
		    $tz[$i]['tid']    = $msql->f('tid');
            $tz[$i]['userid'] = $msql->f('userid');
            $tz[$i]['qishu']  = $msql->f('year') . $msql->f('qishu');
            $tz[$i]['je']     = (float) $msql->f('je');
            if ($uid == $msql->f('userid'))
                $tz[$i]['me'] = 1;
            $tz[$i]['zcje']   = pr2($msql->f('je') * $msql->f($zcstr) / 100);
            $tz[$i]['peilv1'] = (float) $msql->f('peilv1');
            $tz[$i]['peilv2'] = (float) $msql->f('peilv2');
            $tz[$i]['points'] = (float) $msql->f('points');
            $tz[$i]['con']    = $msql->f('content');
			

			
			if ($tmp['b' . $msql->f('gid') . $msql->f('bid')] == '') {
                $tmp['b' . $msql->f('gid') . $msql->f('bid')] = transb8('name', $msql->f('bid'), $msql->f('gid'));
            }
            if ($tmp['s' . $msql->f('gid') . $msql->f('sid')] == '') {
                $tmp['s' . $msql->f('gid') . $msql->f('sid')] = transs8('name', $msql->f('sid'), $msql->f('gid'));
            }
            if ($tmp['c' . $msql->f('gid') . $msql->f('cid')] == '') {
                $tmp['c' . $msql->f('gid') . $msql->f('cid')] = transc8('name', $msql->f('cid'), $msql->f('gid'));
            }
            if ($tmp['p' . $msql->f('gid') . $msql->f('pid')] == '') {
                $tmp['p' . $msql->f('gid') . $msql->f('pid')] = transp8('name', $msql->f('pid'), $msql->f('gid'));
            }
            $tz[$i]['bid']    = $tmp['b' . $msql->f('gid') . $msql->f('bid')];
            $tz[$i]['sid']    =  $tmp['s' . $msql->f('gid') . $msql->f('sid')];
            $tz[$i]['cid']    =  $tmp['c' . $msql->f('gid') . $msql->f('cid')];
            $tz[$i]['pid']    = $tmp['p' . $msql->f('gid') . $msql->f('pid')];
            $tz[$i]['bids']    = $msql->f('bid');
            $tz[$i]['sids']    =  $msql->f('sid');
            $tz[$i]['cids']    =  $msql->f('cid');
            $tz[$i]['pids']    =$msql->f('pid');
            $tz[$i]['fl']    =$fl;
            $tz[$i]['time']   = $msql->f('time');
            $tz[$i]['xtime']  = substr($msql->f('time'),-11);
            $tz[$i]['user']   = transu($msql->f('userid'));
            if ($layer < 9) {
                if ($msql->f('uid' . ($layer + 1)) == 0) {
                    $tz[$i]['duser'] = transu($msql->f('userid'));
                } else {
                    $tz[$i]['duser'] = transu($msql->f('uid' . ($layer + 1)));
                }
            }
            for ($j = 0; $j < 9; $j++) {
                $tz[$i]['zc' . $j] = pr2($msql->f('je') * $msql->f('zc' . $j) / 100);
                if ($j != 0) {
                    $tz[$i]['points' . $j] = (float) $msql->f('points' . $j);
                    $tz[$i]['peilv1' . $j] = (float) $msql->f('peilv1' . $j);

                    if ($msql->f('peilv2' . $j) > 1) {
                        $tz[$i]['peilv1' . $j] .= '/' . (float) $msql->f('peilv2' . $j);
                    }
                }
            }
            if (strpos("|A|B|C|D", $msql->f('abcd')))
                $tz[$i]['abcd'] = $msql->f('abcd');
            else
                $tz[$i]['abcd'] = '';
            if (strpos("|A|B|", $msql->f('ab')))
                $tz[$i]['ab'] = $msql->f('ab');
            else
                $tz[$i]['ab'] = '';
			$tz[$i]['z'] = $msql->f('z');	
            $i++;
        }
        $e = array(
            "tz" => $tz,
            "page" => $pstr,
            'sql' => $sql,
            "layer" => $layer
        );
        echo json_encode($e);
        unset($e);
        break;
    case "deltz":
		$tid = $_POST['tid'];
        $sql = "delete from `$tb_lib` where instr('$tid',concat(id,tid,userid))";
        $msql->query($sql);
		echo 1;
        break;
    case "edittz":
        $str = str_replace('\\', '', $_POST['str']);
        $arr = json_decode($str, true);
        $ca  = count($arr);
        $fl = transgame($gid,'fenlei');
        for ($i = 0; $i < $ca; $i++) {
            $pid = '';
            $msql->query("select * from `$tb_lib` where concat(id,tid,userid)='" . $arr[$i]['tid'] . "'");
            $msql->next_record();
            if(($fl==107 & $msql->f('bid')!=23378805) | ($fl==101 & $msql->f('bid')==23378755)){
                $sid = $arr[$i]['sid']; 
                 $fsql->query("select * from `$tb_sclass` where gid='$gid' and name='$sid' ");
                $fsql->next_record();
                $bid = $fsql->f('bid');
                $sid = $fsql->f('sid');
                $fsql->query("select * from `$tb_play` where gid='$gid' and sid='$sid' and name='".$arr[$i]['pid']."'");
                $fsql->next_record();
                $cid = $fsql->f('cid');
                $pid = $fsql->f('pid');
               $peilv1  = $arr[$i]['peilv1'];
               $points  = $arr[$i]['points'];
               $content = $arr[$i]['con'];
                $je      = $arr[$i]['je'];
               $z      = $arr[$i]['z'];
               $time    = date("Y-m",strtotime($msql->f('time'))) . '-' . $arr[$i]['time'];
              $sql     = '';
               $sql     = "update `$tb_lib` set bid='$bid',sid='$sid',cid='$cid',pid='$pid',peilv1='$peilv1',points='$points',content='$content',je='$je',time='$time',z='$z'";
            }else {
               $pid = untransp($msql->f('bid'), $msql->f('sid'), $msql->f('cid'), $arr[$i]['pid']);
               if ($pid == '')
                continue;
               $peilv1  = $arr[$i]['peilv1'];
               $points  = $arr[$i]['points'];
               $content = $arr[$i]['con'];
                $je      = $arr[$i]['je'];
			   $z      = $arr[$i]['z'];
               $time    = date("Y-m",strtotime($msql->f('time'))) . '-' . $arr[$i]['time'];
              $sql     = '';
               $sql     = "update `$tb_lib` set pid='$pid',peilv1='$peilv1',points='$points',content='$content',je='$je',time='$time',z='$z'";
           }
            for ($j = 1; $j < 9; $j++) {
                $sql .= ",peilv1" . $j . "='" . $arr[$i]['peilv1' . $j] . "'";
                $sql .= ",points" . $j . "='" . $arr[$i]['points' . $j] . "'";
            }
            $sql1 = $sql . ",kk=0 where id='" . $msql->f('id') . "'";
			$sql2 = $sql . ",kk=1 where id='" . $msql->f('id') . "'";
            $fsql->query($sql1);
			$fsql->query($sql2);
        }
  
		echo 1;
		break;
}
?>